﻿using Azure;
using BST250301TestStand.Models.Utils;
using Mysqlx;
using OpenTK.Audio.OpenAL;
using System.Windows.Forms;
using static BST250301TestStand.Models.Utils.PLCConfig;
using static BST250301TestStand.Models.Utils.TCUConfig;

namespace BST250301.Pages
{
    public partial class HomeTop : UserControl
    {
        int stat = 0;
        bool isplc = false;
        //bool ishowing = false;
        public HomeTop()
        {
            InitializeComponent();

            ShowTime();

            PLCConfig.ShowPLCStatus += ShowPLC;
            TCUConfig.ShowTCUStatus += ShowTCU;
            NVHConfig.ShowNVHStatus += ShowNVH;

            System.Windows.Forms.Timer timer = new() { Interval = 100 };

            timer.Tick += (s, e) =>
            {
                ShowTime();
            };
            timer.Start();

            Helpers.Antd.SetBreathingLight(label1, Color.FromArgb(105, 70, 255), Color.Empty, 1);
            Helpers.Antd.SetBreathingLight(lbInfo, Color.FromArgb(105, 70, 255), Color.Empty, 1);
            Helpers.Antd.SetBreathingLight(label2, Color.FromArgb(105, 70, 255), Color.Empty, 1);
        }


        private void ShowTime()
        {
            label2.Text = $"{DateTime.Now:yyyy年M月d日H时m分s秒}";
            //if (MainWindow.PLC != null && MainWindow.PLC.IsConnectPLC)
            //    this.buttonPLC.Type = AntdUI.TTypeMini.Success;
            //else
            //    this.buttonPLC.Type = AntdUI.TTypeMini.Error;


        }
        /// <summary>
        /// 显示PLC在线信息，适合多个PLC连接
        /// </summary>
        /// <param name="isconnected"></param>
        /// <param name="Remotename"></param>
        private void ShowPLC(object? sender, ShowPLCStatusParm plc)
        {
            //    if (this.InvokeRequired)
            //    {
            //        this.BeginInvoke(new EventHandler<ShowPLCStatusParm>(ShowPLC), sender, plc);
            //        return;
            //    }
            //if (ishowing)
            //    return;
            this.Invoke(new MethodInvoker(async () =>
            {
                //ishowing = true;
                if (!plc.IsOnline)
                {
                    isplc = false;
                    //for (int i = 0; i < 3; i++)
                    {
                        //await Task.Delay(500);
                        this.buttonPLC.Type = AntdUI.TTypeMini.Error;
                        this.buttonPLC.Text = $"PLC离线";
                        //await Task.Delay(500);
                        //this.buttonPLC.Type = AntdUI.TTypeMini.Default;
                    }
                }
                else
                {
                    if (!isplc)
                    {
                        isplc = true;
                        this.buttonPLC.Type = AntdUI.TTypeMini.Success;
                        this.buttonPLC.Text = $"PLC在线";
                    }
                }
                //ishowing = false;
            }));

        }
        private void ShowTCU(object? sender, ShowTCUStatusParm tcu)
        {
            if (this.InvokeRequired)
            {
                this.BeginInvoke(new EventHandler<ShowTCUStatusParm>(ShowTCU), sender, tcu);
                return;
            }
            else if (!tcu.IsOnline)
            {
                for (int i = 0; i < 3; i++)
                {
                    Thread.Sleep(100);
                    this.buttonTCU.Type = AntdUI.TTypeMini.Error;
                    this.buttonTCU.Text = $"TCU离线";
                    Thread.Sleep(100);
                    this.buttonTCU.Type = AntdUI.TTypeMini.Default;
                }
            }
            else if (tcu.IsOnline && BST250301TestStand.Models.Operation.IsTCUOnline)
            {
                this.buttonTCU.Type = AntdUI.TTypeMini.Success;
                this.buttonTCU.Text = $"TCU在线";
            }
            else
            {
                this.buttonTCU.Type = AntdUI.TTypeMini.Info;
                this.buttonTCU.Text = $"检查TCU接线";
            }
        }
        private void ShowNVH(object? sender, ShowNVHStatusParm nvh)
        {
            if (this.InvokeRequired)
            {
                this.BeginInvoke(new EventHandler<ShowNVHStatusParm>(ShowNVH), sender, nvh);
                return;
            }
            else if (!nvh.IsOnline)
            {
                for (int i = 0; i < 3; i++)
                {
                    Thread.Sleep(100);
                    this.buttonNVH.Type = AntdUI.TTypeMini.Error;
                    this.buttonNVH.Text = $"NVH离线";
                    Thread.Sleep(100);
                    this.buttonNVH.Type = AntdUI.TTypeMini.Default;
                }
            }
            else if (nvh.IsOnline)
            {
                this.buttonNVH.Type = AntdUI.TTypeMini.Success;
                this.buttonNVH.Text = $"NVH在线";
            }
            else
            {
                this.buttonNVH.Type = AntdUI.TTypeMini.Info;
                this.buttonNVH.Text = $"检查NVH接线";
            }
        }
    }
}